﻿namespace BeatEmUp.World
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using BeatEmUp.Menu;
    using BeatEmUp.Characters.Enemies;
    using BeatEmUp.HUD;
    using BeatEmUp.Items.Weapons;
    using BeatEmUp.Enums;
    using BeatEmUp.World;
    using BeatEmUp.World.Drawable;
    using Microsoft.Xna.Framework.Content;

    class ScreenFactory
    {
        public Screen CreateScreenForLevel(Level level, GameManager gameManager, int worldPosition = 0)
        {
            Screen createdScreen = null;

            switch (level)
            {
                case Level.One:
                    createdScreen = this.CreateLevelOne(gameManager);
                    break;
                case Level.Two:
                    createdScreen = this.CreateLevelTwo(gameManager);
                    break;
                default:
                    throw new Exception("Unknown level");
            }

            if (0 != worldPosition)
                createdScreen.MoveTo(worldPosition);

            return createdScreen;
        }

        protected Screen CreateLevelTwo(GameManager gameManager)
        {
            ConfigBuilder<Element, ElementFactory> backgroundBuilder = new ConfigBuilder<Element, ElementFactory>(gameManager);
            ConfigBuilder<StaticElement, StaticElementFactory> staticElementsBuilder = new ConfigBuilder<StaticElement, StaticElementFactory>(gameManager);
            Drawable.IDrawable drawable;
            Drawable.IDrawable[] drawables;
            float height;

            Drawable.IDrawable background = StaticDrawable.CreateUsingSegment(@"Sprites\World\Level 1\sky", new Rectangle(0, 150, 1024, 718));

            drawable = StaticDrawable.CreateInSizeUsingSegment(@"Sprites\World\Level 1\grass", new Vector2(200, 80), new Rectangle(0, 0, 1024, 400));
            height = gameManager.WorldHeight + gameManager.HudHeight - 300;
            backgroundBuilder.AddRepeatingElement(new Vector2(0, height), 40, 100, 4, 79, drawable, 0);

            drawable = StaticDrawable.CreateInSize(@"Sprites\World\Level 1\pavement", new Vector2(128, 101));
            height = gameManager.WorldHeight + gameManager.HudHeight - 200;
            backgroundBuilder.AddHorizontalRepeatingElement(new Vector2(0, height), 32, 128, drawable, 0.1f);


            drawable = new StaticDrawable(@"Sprites\StaticItems\bush");
            height = gameManager.WorldHeight + gameManager.HudHeight - 230;
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(5, height), 21, 200, drawable, 0.3f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(5, height + 100), 21, 200, drawable, 0.3f);

            drawables = new Drawable.IDrawable[3] {
                new StaticDrawable(@"Sprites\StaticItems\deadTree"),
                new StaticDrawable(@"Sprites\StaticItems\deadTree2"),
                new StaticDrawable(@"Sprites\StaticItems\tree")
            };

            height = gameManager.WorldHeight + gameManager.HudHeight - 320;
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(-100, height), 40, 100, 150, drawables, 0.25f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(-110, height + 20), 40, 40, 100, drawables, 0.25f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(-110, height + 40), 40, 100, 150, drawables, 0.25f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(-110, height + 250), 40, 100, 150, drawables, 0.25f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(-110, height + 270), 40, 40, 100, drawables, 0.25f);

            return new Screen(gameManager, background, backgroundBuilder.GetConfig(), staticElementsBuilder.GetConfig(), 3000);
        }

        protected Screen CreateLevelOne(GameManager gameManager)
        {
            ConfigBuilder<Element, ElementFactory> backgroundBuilder = new ConfigBuilder<Element, ElementFactory>(gameManager);
            ConfigBuilder<StaticElement, StaticElementFactory> staticElementsBuilder = new ConfigBuilder<StaticElement, StaticElementFactory>(gameManager);
            Drawable.IDrawable drawable;
            float baseHeight;
            string basePath = @"Sprites\World\Level 2\";

            Drawable.IDrawable background = StaticDrawable.CreateInSize(basePath + "moon", new Vector2(1024, 768));

            drawable = new StaticDrawable(basePath + "rockA");
            baseHeight = gameManager.HudHeight + gameManager.WorldHeight - 320;
            backgroundBuilder.AddHorizontalRepeatingElement(new Vector2(0, baseHeight), 6, 1129, drawable, 0.3f);
            backgroundBuilder.AddHorizontalRepeatingElement(new Vector2(-250, baseHeight+115), 6, 1129, drawable, 0.31f);
            backgroundBuilder.AddHorizontalRepeatingElement(new Vector2(-500, baseHeight + 230), 6, 1129, drawable, 0.32f);

            baseHeight = gameManager.WorldHeight + gameManager.HudHeight - 230;
            drawable = AnimootedDrawable.CreateWithStartingPoint(@"Sprites\StaticItems\torchSprite", 8, new Vector2(18, 53), new Vector2(2, 0), 0.16f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight - 90), 1, 35, drawable, 0.32f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight - 60), 2, 35, drawable, 0.33f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight - 30), 3, 35, drawable, 0.34f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight), 4, 35, drawable, 0.35f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight + 100), 4, 35, drawable, 0.35f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight + 130), 3, 35, drawable, 0.36f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight + 160), 2, 35, drawable, 0.37f);
            staticElementsBuilder.AddHorizontalRepeatingElement(new Vector2(10, baseHeight + 190), 1, 35, drawable, 0.38f);

            return new Screen(gameManager, background, backgroundBuilder.GetConfig(), staticElementsBuilder.GetConfig(), 5000);
        }
    }
}
